package cate.game.common;

import io.netty.util.concurrent.FastThreadLocal;

import java.util.Objects;
import java.util.Optional;

public class FastOpaqueStorage implements AutoCloseable {

    static final FastThreadLocal<Object> LOCAL_STORAGE = new FastThreadLocal<>();

    public FastOpaqueStorage(Object opaque) {
        LOCAL_STORAGE.set(Objects.requireNonNull(opaque));
    }

    public static Optional<Object> current() {
        return Optional.ofNullable(LOCAL_STORAGE.get());
    }

    @Override
    public void close() throws Exception {
        LOCAL_STORAGE.remove();
    }
}
